export default {
	namespaced: true,
	state: {
		globalEventList: [], // 全局激活的事件
		sendCompIds: [],
		eventCompIds: [],
		currentEventParams: {} // 当前事件参数
	},
	mutations: {
		// 注册事件
		registerEvent(state, data) {
			if (data && data.length > 0) {
				for (let i = 0; i < data.length; i++) {
					const item = data[i];
					let findIndex = state.globalEventList.findIndex(event => {
						return event.eventId == item.eventId;
					});
					let { compId, eventId, eventName, eventType, targetCompId, args, linkUrl = '', customFn } = item;
					if (findIndex > -1) {
						state.globalEventList.splice(findIndex, 1, {
							compId,
							eventId,
							eventName,
							eventType,
							targetCompId,
							linkUrl,
							customFn,
							args: JSON.parse(args || '{}')
						});
					} else {
						state.globalEventList.push({
							compId,
							eventId,
							eventName,
							eventType,
							targetCompId,
							linkUrl,
							customFn,
							args: JSON.parse(args || '{}')
						});
					}
				}
				state.sendCompIds = state.globalEventList.map(global => global.targetCompId);
				state.eventCompIds = state.globalEventList.map(global => global.eventId);
			}
		},
		// 卸载事件
		unloadEvent(state, data) {
			if (data && data.length > 0) {
				data.map(d => {
					let findIndex = state.globalEventList.findIndex(event => {
						return event.eventId == d.eventId;
					});
					if (findIndex > -1) {
						state.globalEventList.splice(findIndex, 1);
					}
					let sendIndex = state.sendCompIds.findIndex(sId => {
						return sId == d.targetCompId;
					});
					let eventIndex = state.eventCompIds.findIndex(eId => {
						return eId == d.eventId;
					});
					state.sendCompIds.splice(sendIndex, 1);
					state.eventCompIds.splice(eventIndex, 1);
				});
			}
		},
		// 注册当前事件参数
		registerEventParams(state, data) {
			// APPEND_PARAMS为事件追加参数
			if (data.APPEND_PARAMS) {
				state.currentEventParams = {
					...state.currentEventParams,
					...data
				};
			} else {
				state.currentEventParams = data;
			}
		},
		// 卸载当前事件参数
		unloadEventParams(state, data) {
			state.currentEventParams = {};
		}
	},
	actions: {}
};
